// compontnts/input/lt-input.js
import {formVerify,setDictTextInfo} from "../../../utils/util"
import {getDictInfo} from "../../../api/dict"
const defaultSettings = require('../../../config/defaultSettings')
Component({
  options: {
    styleIsolation: 'shared',
  },
  /**
   * 组件的属性列表
   */
  properties: {
    formData:{
      type:Object,
      value:{}
    },
    field:{
      type:Object,
      value:{}
    },
    betweenSign:{
      type:String,
      value:""
    },
   
  },
  lifetimes: {
    ready:function(){
      
    },
    attached: async function() {
      // 在组件实例进入页面节点树时执行,初始化一些操作
      if(this.data.field['clean']){
        this.setData({cancelButtonText:'清空'})
      }
      await this.initDictData()
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  observers:{
    'formData':function(formData){
      if(formData[this.data.field['field']+this.data.betweenSign]){
        let val = formData[this.data.field['field']+this.data.betweenSign]
        this.setData({cascaderValue:val})
        this.setData({value:getApp().globalData.dictTextInfo[this.data.field['dictCode']+"#"+val]})
      }else{
        this.setData({value:""})
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    errorMessage:"",
    isShow:false,
    dictOptions: [],
    resultOptions:[],
    value:null,
    cancelButtonText:'取消',
    fieldNames: {
      text: 'title',
      value: 'value',
      children: 'children',
    },
    cascaderValue:'',
    primaryColor:defaultSettings.primaryColor,
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    async initDictData(){
      if(this.data.field["options"] && this.data.field["options"].length>0){
        this.setData({dictOptions:this.data.field["options"]})
        return
      }
      if(!this.data.field['dictCode']){
        return
      }
      let that = this
      if(getApp().globalData.dictInfo[that.data.field['dictCode']]){
        this.setData({dictOptions:Object.assign([],getApp().globalData.dictInfo[that.data.field['dictCode']])})
        return
      }
      await getDictInfo(this.data.field['dictCode'],this.data.field['dictParam'] || '').then(res=>{
        if(res.success){
          that.data.resultOptions = res.result[that.data.field['dictCode']]
          this.setData({dictOptions:Object.assign([],that.data.resultOptions)})
          getApp().globalData.dictInfo[that.data.field['dictCode']] = that.data.resultOptions
          setDictTextInfo(this.data.field['dictCode'])
        }else{
          wx.showToast({
            icon:"error",
            title: res.message,
          })
        }
      })
    },
 
    showPicker(){
      if(this.data.field['disabled']){
        this.setData({ isShow: false });
      }else{
        this.setData({ isShow: !this.data.isShow });
      }
    },
    onChange(event){
      //表示只有层级选完才赋值
      if(this.data.field["finish"]){
        return false
      }
      this.data.cascaderValue = event.detail.value
      this.setData({value:getApp().globalData.dictTextInfo[this.data.field['dictCode']+"#"+event.detail.value]})
      //将输入框改变的值实时赋值给formData
      this.data.formData[this.data.field['field']+this.data.betweenSign]=this.data.cascaderValue
      //调用父级，将值传出
      this.triggerEvent("change",{field:this.data.field['field']+this.data.betweenSign,val:this.data.cascaderValue})
      this.verify()
    },
    onClose(){
      this.setData({ isShow: false });
    },
    onCancel(){
      this.setData({ isShow: false });
      if(this.data.field['clean']){
        this.setData({value:''})
        //将输入框改变的值实时赋值给formData
        this.data.formData[this.data.field['field']+this.data.betweenSign]=''
        //调用父级，将值传出
        this.triggerEvent("change",{field:this.data.field['field']+this.data.betweenSign,val:''})
      }
      this.verify()
    },
    onBlur(e){
      this.verify()
    },
    onFinish(e) {
      this.setData({ isShow: false });
      const { selectedOptions, value } = e.detail;
      this.data.cascaderValue = value
      this.setData({value:getApp().globalData.dictTextInfo[this.data.field['dictCode']+"#"+value]})
      //将输入框改变的值实时赋值给formData
      this.data.formData[this.data.field['field']+this.data.betweenSign]=this.data.cascaderValue
      //调用父级，将值传出
      this.triggerEvent("change",{field:this.data.field['field']+this.data.betweenSign,val:this.data.cascaderValue})
      this.verify()
    },
    verify(){
      return formVerify(this)
    }
  }
})
